package com.zhu.classpath.entry.base;

import com.zhu.utils.ClassNameUtil;

import java.io.*;

//目录class文件提取器
public class DirEntry extends BaseEntry{

   private DirEntry(String path){
       super(path);
       // 验证是否为有效目录
       File dir = new File(this.absPath);
       if (!dir.exists()) {
           throw new IllegalArgumentException("Directory does not exist: " + path);
       }
       if (!dir.isDirectory()) {
           throw new IllegalArgumentException("Not a directory: " + path);
       }
   }

   public static DirEntry createDirEntry(String path){
       if (path ==null || path.isEmpty()){
           return null;
       }
       return new DirEntry(path);
   }

    @Override
    public InputStream readClass(String className) {
        // 参数校验
        if (className == null || className.isEmpty()) {
            return null;
        }
        //className 是类的全限定名（如 java/lang/String）。
        //将类名转换为文件路径 java/lang/String → java\lang\String.class（Windows）。
        className = ClassNameUtil.normalizeClassName(className);
        File file = new File(this.absPath, className);
        if (file.exists() && file.isFile()) {
            try {
                return new FileInputStream(file);
            } catch (Exception e) {
                return null;
            }
        }
        return null;
    }
}
